<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <a-spin :spinning="isLoading">
      <a-form :form="form" @submit="handleSubmit">
        <a-form-item label="商城名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['store_name', { rules: [{ required: false, min: 2, message: '请输入至少2个字符' }] }]" />
        </a-form-item>
        <a-form-item label="商城简介" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['describe']" />
        </a-form-item>
        <a-form-item label="商城Logo" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="建议尺寸: 300*300">
          <SelectImage :defaultList="record.logoImage ? [record.logoImage] : []" v-decorator="['logo_image_id']" />
        </a-form-item>
        <a-form-item label="是否营业" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['is_open']">
            <a-radio :value="1">营业</a-radio>
            <a-radio :value="0">闭店</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="营业时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-time-picker v-model="defaultValue1" format="HH:mm" mode="range" placeholder="开始时间"
            @change="rangeChange1" />
          &nbsp;至&nbsp;
          <a-time-picker v-model="defaultValue2" format="HH:mm" mode="range" placeholder="结束时间"
            @change="rangeChange2" />
        </a-form-item>
        <a-form-item label="进群二维码" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="建议尺寸: 300*300">
          <SelectImage :defaultList="record.qrImage ? [record.qrImage] : []" v-decorator="['qr_image_id']" />
        </a-form-item>
        <a-form-item :wrapperCol="{ span: wrapperCol.span, offset: labelCol.span }">
          <a-button type="primary" html-type="submit">提交</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/store'
import { SelectImage } from '@/components'
import moment from 'moment'
export default {
  components: {
    SelectImage
  },
  data() {
    return {
      // 标签布局属性
      labelCol: { span: 4 },
      // 输入框布局属性
      wrapperCol: { span: 10 },
      // loading状态
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录详情
      record: {},

      defaultValue1: null, // 营业开始时间
      defaultValue2: null, // 营业结束时间

      start_time: null,
      end_time: null,
    }
  },
  // 初始化数据
  created() {
    // 获取当前详情记录
    this.getDetail()
  },
  methods: {
    moment,
    // 获取当前详情记录
    getDetail() {
      this.isLoading = true
      Api.info()
        .then(result => {
          // 当前记录
          this.record = result.data.storeInfo

          this.defaultValue1 = moment(this.record.start_time, 'HH:mm'); // 营业开始时间
          this.defaultValue2 = moment(this.record.end_time, 'HH:mm'); // 营业结束时间

          this.start_time = this.record.start_time
          this.end_time = this.record.end_time

          // 设置默认值
          this.setFieldsValue()
        })
        .finally(() => this.isLoading = false)
    },

    // 设置默认值
    setFieldsValue() {
      const { record, form: { setFieldsValue } } = this
      // 表单内容
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['store_name', 'describe', 'is_open', 'logo_image_id', 'qr_image_id']))
      })
    },

    // 开始时间
    rangeChange1(time) {
      this.start_time = time ? time.format('HH:mm') : null;
    },
    // 结束时间
    rangeChange2(time) {
      this.end_time = time ? time.format('HH:mm') : null;
    },

    // 确认按钮
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    // 提交到后端api
    onFormSubmit(values) {
      let start_time = Number(this.start_time.replace(':', ''))
      let end_time = Number(this.end_time.replace(':', ''))

      if (start_time > end_time) {
        this.$message.error('结束时间不能早于开始时间')
      } else {
        values.start_time = this.start_time //营业开始时间
        values.end_time = this.end_time //营业结束时间

        this.isLoading = true
        Api.update({ form: values })
          .then(result => this.$message.success(result.message, 1.5))
          .finally(() => this.isLoading = false)
      }
    }
  }
}
</script>
<style lang="less" scoped>
/deep/.ant-form-item-control {
  padding-left: 10px;

  .ant-form-item-control {
    padding-left: 0;
  }
}
</style>
